package ecom.beans;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@Table(name="ProductStore")
public class ProductStoreBean implements java.io.Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private int                 id;
    @Column(name="location")
    private String              location;
    @ManyToOne
    @JoinColumn(name="accountId")
    private int                 account;
    @Column(name="name")
    private String              name;
    @OneToMany
    private Collection <ProductBean> products;
    
    public ProductStoreBean(){
    }
    
    public ProductStoreBean(int id,String location,int account,String name){
	this.setId(id);
	this.setName(name);
	this.setAccount(account);
	this.setLocation(location);
	this.products  = new ArrayList<ProductBean>();
    }
    public ProductStoreBean(ProductStoreBean ps){
	setId(ps.getId());
	setLocation(ps.getLocation());
	setAccount(ps.getAccount());
	setName(ps.getName());
    }
    
    public int getId(){
	return this.id;
    }
    public void setId(int id){
	this.id = id;
    }
    public void setLocation(String location){
	this.location = location;
    }
    public String getLocation(){
	return this.location;
    }
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="ProductStore")
    public Collection<ProductBean> getProducts(){
	return this.products;
    }
    public void setProducts(Collection<ProductBean> products){
	this.products = products;
    }
    public void setName(String name){
	this.name = name;
    }
    public String getName(){
	return this.name;
    }
    public void setAccount(int id){
	this.account = id;
    }
    public int getAccount(){
	return this.account;
    }
    
    public void addProduct(String product,double price)
    {
	if (this.products == null) this.products = new ArrayList<ProductBean>();
	ProductBean item = new ProductBean();
	item.setProductStore(this.id);
	item.setName(product);
	item.setPrice(price);
	this.products.add(item);
    }
    public void remProduct(ProductBean item)
    {
	this.products.remove(item);
    }
    @Override
    public String toString(){
	return "["+getId()+","+getName()+"-"+getLocation()+","+getAccount()+"-"+getProducts().size()+"]";
    }
    
}